﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace ProductManager
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");

            if (!checkConnection())
            {
                gui.GUIConfig config = new gui.GUIConfig();
                config.ShowDialog();
            }

            if (checkConnection())
            {
                Application.Run(new ProductManager());
            }
            
        }

        private static bool checkConnection()
        {
            bool check = false;

            try
            {
                string s = "";
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                s = config.ConnectionStrings.ConnectionStrings["ProductManager.Properties.Settings.ProductManagersConnectionString"].ConnectionString;
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(s);
                try
                {
                    conn.Open();
                    check = true;
                }
                catch (Exception ex)
                {
                    check = false;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                check = false;
            }

            return check;
        }
    }
}
